package com.arthas.arthasforsba.utils.file;

import com.arthas.arthasforsba.exception.BaseException;
import org.apache.commons.lang3.StringUtils;

/**
 * 媒体类型工具类
 *
 * @author ruoyi
 */
public class MimeTypeUtils
{
    public static final String IMAGE_PNG = "image/png";

    public static final String IMAGE_JPG = "image/jpg";

    public static final String IMAGE_JPEG = "image/jpeg";

    public static final String IMAGE_BMP = "image/bmp";

    public static final String IMAGE_GIF = "image/gif";

    public static final String PDF = "application/pdf";

    public static final String VIDEO_MP4 = "video/mp4";

    public static final String VIDEO_AVI = "video/avi";

    public static final String VIDEO_MOV = "video/mov";

    public static final String VIDEO_MPG = "video/mpg";

    public static final String[] IMAGE_EXTENSION = { "bmp", "gif", "jpg", "jpeg", "png"};

    public static final String[] JAVA_EXTENSION = { "java", "class"};

    public static final String[] PREVIEW_EXTENSION = {"pdf", "bmp", "gif", "jpg", "jpeg", "png", "mp4", "avi", "mov", "mpg"};

    public static final String[] FLASH_EXTENSION = { "swf", "flv" };

    public static final String[] MEDIA_EXTENSION = { "swf", "flv", "mp3", "wav", "wma", "wmv", "mid", "avi", "mpg",
            "asf", "rm", "rmvb" };

    public static final String[] DEFAULT_ALLOWED_EXTENSION = {
            // 图片
            "bmp", "gif", "jpg", "jpeg", "png",
            // word excel powerpoint
            "doc", "docx", "xls", "xlsx", "ppt", "pptx", "html", "htm", "txt",
            // 压缩文件
            "rar", "zip", "gz", "bz2",
            // pdf
            "pdf", "wps", "ofd", "avi", "rmvb", "rm", "asf", "divx",
            "mpg", "mpeg", "mpe", "wmv", "mp4", "mkv", "vob", "mov" };

    public static boolean validPreviewExtension(String extension) {
        for (String ex : PREVIEW_EXTENSION) {
            if (ex.equals(StringUtils.lowerCase(extension))) {
                return true;
            }
        }
        return false;
    }

    public static boolean validImageExtension(String extension) {
        for (String ex : IMAGE_EXTENSION) {
            if (ex.equals(extension)) {
                return true;
            }
        }
        return false;
    }

    public static String getExtensionType(String extension) {
        switch (StringUtils.lowerCase(extension)) {
            case "png":
                return IMAGE_PNG;
            case "jpg":
                return IMAGE_JPG;
            case "jpeg":
                return IMAGE_JPEG;
            case "bmp":
                return IMAGE_BMP;
            case "gif":
                return IMAGE_GIF;
            case "pdf":
                return PDF;
            case "mp4":
                return VIDEO_MP4;
            case "avi":
                return VIDEO_AVI;
            case "mov":
                return VIDEO_MOV;
            case "mpg":
                return VIDEO_MPG;
            default:
                throw new BaseException("暂不支持，该类型:{}",extension);
        }
    }

    public static String getExtension(String prefix)
    {
        switch (prefix)
        {
            case IMAGE_PNG:
                return "png";
            case IMAGE_JPG:
                return "jpg";
            case IMAGE_JPEG:
                return "jpeg";
            case IMAGE_BMP:
                return "bmp";
            case IMAGE_GIF:
                return "gif";
            default:
                return "";
        }
    }
}
